package cn.etgps.etgps.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;

import cn.etgps.etgps.R;
import cn.etgps.etgps.base.BaseActivity;
import cn.etgps.etgps.dialog.DialogCreater;
import cn.etgps.etgps.frgament.GarageFragment;

public class MainActivity extends BaseActivity {
    /**
     * Manger of the fragment
     */
    private FragmentManager mFragmentManager = null;
    /**
     * The Fragment transaction
     */
    private FragmentTransaction mFragmentTransaction = null;
    private GarageFragment mGarageFragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mGarageFragment = new GarageFragment();
        mGarageFragment.setParentHandler(null, this);
        mFragmentManager = this.getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.add(R.id.main_view, mGarageFragment);
        mFragmentTransaction.commitAllowingStateLoss();
    }

    @Override
    public void clickButtonListener(String name) {
        super.clickButtonListener(name);
        if (name.equals(getString(R.string.title_btn_map_mode))) {
            Intent intent = new Intent(this, MapMarkerActivity.class);
            startActivity(intent);
        } else {
            Intent intent = new Intent(this, MeActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            try {
                this.showNormalDialog(DialogCreater.DIALOG_EXIT);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }
        return super.dispatchKeyEvent(event);
    }
}

